#!/bin/bash

rm -rf hosts.tmp
touch hosts.tmp

cat >>hosts.tmp <<EOF
192.168.56.101 root root
EOF

rm -rf copykey.sh
touch copykey.sh
cat >>copykey.sh <<EDF
#!/bin/bash
if [ ! -f ~/.ssh/id_rsa ];then
 ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa
else
 echo "id_rsa has created ..."
fi

#分发到各个节点
while read line
  do
    user=\$(echo \$line | cut -d " " -f2)
    ip=\$(echo \$line | cut -d " " -f1)
    passwd=\$(echo \$line | cut -d " " -f3)
    expect <<EOF
      set timeout 10
      spawn ssh-copy-id \$user@\$ip
      expect {
        "yes/no" { send "yes\n";exp_continue }
        "password" { send "\$passwd\n" }
      }
     expect "password" { send "\$passwd\n" }
     
EOF
  ssh \$user@\$ip "echo \$PATH"
  done <  hosts.tmp
EDF

chmod +x ./copykey.sh
./copykey.sh
